<?php
/**
 * 测试分销佣金冻结功能修复
 */

// 定义目录分隔符
define('DS', DIRECTORY_SEPARATOR);

// 获取当前脚本所在目录
$currentDir = __DIR__;

// 定义项目根目录（向上三级目录）
$rootPath = dirname($currentDir, 3) . DS;

// 引入ThinkPHP基础文件
require $rootPath . 'thinkphp' . DS . 'base.php';

use addons\shopro\model\commission\Agent as AgentModel;
use addons\shopro\model\commission\Reward as RewardModel;

echo "开始测试分销佣金冻结功能修复...\n";

// 测试分销商ID
$agentId = 1; // 请根据实际情况修改

// 获取分销商信息
$agent = AgentModel::get($agentId);
if (!$agent) {
    echo "未找到分销商ID: $agentId\n";
    exit;
}

echo "分销商当前订单计数: " . ($agent->commission_order_count ?? 0) . "\n";

// 模拟增加订单计数
$currentOrderCount = $agent->commission_order_count ?? 0;
$newOrderCount = $currentOrderCount + 1;
$agent->commission_order_count = $newOrderCount;
$agent->save();

echo "增加订单后计数: $newOrderCount\n";

// 检查是否满足释放条件
$isReleaseConditionMet = ($newOrderCount > 0 && $newOrderCount % 3 == 0);
echo "是否满足释放条件: " . ($isReleaseConditionMet ? "是" : "否") . "\n";

// 如果满足条件，模拟释放冻结的佣金
if ($isReleaseConditionMet) {
    // 获取该分销商所有冻结的佣金记录
    $frozenRewards = RewardModel::all([
        'agent_id' => $agentId,
        'status' => RewardModel::COMMISSION_REWARD_STATUS_FROZEN,
    ]);
    
    echo "找到 " . count($frozenRewards) . " 条冻结的佣金记录\n";
    
    // 模拟释放所有冻结的佣金
    foreach ($frozenRewards as $reward) {
        echo "模拟释放佣金记录ID: " . $reward->id . ", 金额: " . $reward->commission . "\n";
        // 实际释放逻辑在Reward.php的releaseCommissionReward方法中实现
    }
}

echo "测试完成！\n";